/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package majordome.views;

import com.google.gdata.data.DateTime;
import com.google.gdata.util.ServiceException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import majordome.controller.Controller;

/**
 *
 * @author Valère
 */
public class TextView 
{
    private static Controller controller;
    
    public static void main(String[] args) 
    {
        boolean quit = false;
        boolean connected = false;
        controller = null;
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("----------------------------------------------");
        System.out.println("-------------------Majordome------------------");
        System.out.println("----------------------------------------------");
        
        while(!connected)
        {
            try {
                String username;
                String password;
                System.out.println("Enter your username :");
                username = scanner.nextLine();
                System.out.println("Enter your password :");
                password = scanner.nextLine();
                controller = Controller.connect(username, password);
                connected = true;
            } 
            catch (ServiceException ex) 
            {
                connected = false;
            }
        }
        
        while(!quit)
        {
            String command;
            System.out.println("You:");
            command = scanner.nextLine();
            executeCommand(command);
        }
    }
    
    private static void executeCommand(String command)
    {
        if(command != null)
        {
            switch(command)
            {
                case "calendars":
                    List<String> calendars = controller.getCalendars();
                    System.out.println("Majordome: Your calendars are:");
                    for(String s : calendars)
                    {
                        System.out.println("\t" + s);
                    }
                    break;
                    
                case "contacts":
                    List<String> contacts = controller.getContacts();
                    System.out.println("Majordome: Your contacts are:");
                    for(String s : contacts)
                    {
                        System.out.println("\t" + s);
                    }
                    break;
                    
                case "events":
                    DateTime begin = new DateTime(new Date(2012, 11, 26, 00, 00, 00));
                    DateTime end = new DateTime(new Date(2012, 12, 2, 00, 00, 00));
                    
                    List<String> events = controller.getEvents(begin,end);
                    System.out.println("Majordome: Your events are:");
                    for(String s : events)
                    {
                        System.out.println("\t" + s);
                    }
                    break;
                    
                case "change username":
                    changeUsername();
                    break;
                    
                case "quit":
                    System.out.println("Bye bye sir");
                    System.exit(0);
                    break;
                    
                default:
                    System.out.println("Sorry I don't understand");
                    break;
            }
        }
    }
    
    private static void changeUsername()
    {
        System.out.print("Are you sure? [Y/N]:");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.nextLine();
        
        if(choice.equalsIgnoreCase("Y"))
        {
            boolean connected = false;

            while(!connected)
            {
                try
                {
                    String username;
                    String password;
                    System.out.println("Enter your username :");
                    username = scanner.nextLine();
                    System.out.println("Enter your password :");
                    password = scanner.nextLine();
                    controller = Controller.connect(username, password);
                    connected = true;
                    System.out.println("It's good sir you are now connected");
                } 
                catch (ServiceException ex) 
                {
                    connected = false;
                }
            }
        }
        else if(choice.equalsIgnoreCase("N"))
        {
            System.out.println("Sorry sir");
        }
    }
}
